/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.freedoit.gui;

import java.awt.Color;
import java.sql.Date;
import java.sql.Time;
import java.util.Calendar;
import org.freedoit.FreeDoitConfig;
import org.freedoit.om.Task;
import org.freedoit.util.Constants;
import org.freedoit.util.Utils;

/**
 *
 * @author wanglei
 */
public class TaskItemPanel extends javax.swing.JPanel {
    /**
     * Creates new form NewJPanel
     */
    private Task task;
    public TaskItemPanel(Task task) {
        this.task = task;
        initComponents();
        settingTask();
    }
    
    private void settingTask() {
        jLabelTaskName.setText(task.getTaskName());
        jLabelTaskStatus.setText(calcTaskStatus(jLabelTaskStatus, task));

        String date = calcTaskDate(jLabelDate, task);
        if (date == null || "".equals(date)) {
            hideJLabelDateGap(); // Hide the gap if not date displayed
        }
        jLabelDate.setText(date);

        jCheckBoxTaskFinished.setSelected(task.getFinished());
        jCheckBoxTaskFinished.setActionCommand("" + task.getTaskId());
    }
    
    public Task getTask() {
        return this.task;
    }
    
    private static String calcTaskDate(javax.swing.JLabel jLabelDate, Task task)  {
        Date myDate = task.getStartDate();
        if (task.getFinished()) {
            myDate = task.getFinishDate();
        }

        if (myDate == null)  {
            return "";
        } else {
            Calendar c = Calendar.getInstance();
            Calendar today = Calendar.getInstance();
            c.setTime(myDate);
            
            if (today.before(c)) {
                jLabelDate.setForeground(Color.BLUE);
            } else {
                jLabelDate.setForeground(Color.ORANGE);
            }

            if (c.get(Calendar.YEAR) == today.get(Calendar.YEAR)) {
                if (today.get(Calendar.MONTH) == c.get(Calendar.MONTH) &&
                        today.get(Calendar.DAY_OF_MONTH) == c.get(Calendar.DAY_OF_MONTH)) {
                    return "";
                } else {
                    return Constants.dateSdf.format(myDate);
                }
            } else {
                return Constants.yearSdf.format(myDate);
            }
        }
    }
    
    private static String calcTaskStatus(javax.swing.JLabel jLabelTaskStatus, Task task)  {
        Date startDate = task.getStartDate();
        Date dueDate = task.getDueDate();
        
        if (startDate == null || dueDate == null)  {
            return "";
        }
        boolean taskFinished = task.getFinished();
        Date myDate = new Date(System.currentTimeMillis());
        Time myTime = new Time(System.currentTimeMillis());
        if (taskFinished) {
            myDate = task.getFinishDate();
            myTime = task.getFinishTime();
        }
        
        String status = "";
        if (myDate != null) {
            int cmp = Utils.compareSQLDate(dueDate, myDate);
            if (cmp > 0) {
                if (!taskFinished) {
                    long time = dueDate.getTime() - myDate.getTime();
                    long days = time / 86400000L;
                    status = java.text.MessageFormat.format(
                                    FreeDoitConfig.getText("frame.doit.task.days.left"), 
                                    days);
                    jLabelTaskStatus.setForeground(Color.BLUE);
                }
            } else if (cmp < 0) {
                status = FreeDoitConfig.getText("frame.doit.task.over.due");
                jLabelTaskStatus.setForeground(Color.ORANGE);
            } else {
                Time dueTime = task.getDueTime();
                if (dueTime == null || myTime == null || Utils.compareSQLTime(myTime, dueTime) > 0) {
                    status = FreeDoitConfig.getText("frame.doit.task.over.due");
                    jLabelTaskStatus.setForeground(Color.ORANGE);
                } else {
                    if (!taskFinished) {
                        status = java.text.MessageFormat.format(
                                    FreeDoitConfig.getText("frame.doit.task.due.at"), 
                                    Constants.timeSdf.format(dueTime));
                        jLabelTaskStatus.setForeground(Color.ORANGE);
                    }
                }
            }
        }
        return status;
    }
    
    javax.swing.JCheckBox getJCheckBoxTaskFinished() {
        return jCheckBoxTaskFinished;
    }
    
    javax.swing.JLabel getJLabelDate() {
        return jLabelDate;
    }
    
    javax.swing.JLabel getJLabelTaskName() {
        return jLabelTaskName;
    }
    
    javax.swing.JLabel getJLabelTaskStatus() {
        return jLabelTaskStatus;
    }
    

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jCheckBoxTaskFinished = new javax.swing.JCheckBox();
        jLabelTaskStatus = new javax.swing.JLabel();
        jLabelTaskName = new javax.swing.JLabel();
        jLabelDate = new javax.swing.JLabel();
        jLabelDateGap = new javax.swing.JLabel();

        setBackground(new java.awt.Color(255, 255, 255));
        setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(218, 218, 242)));
        setMaximumSize(new java.awt.Dimension(705, 39));
        setMinimumSize(new java.awt.Dimension(705, 39));
        setPreferredSize(new java.awt.Dimension(705, 39));

        jCheckBoxTaskFinished.setBackground(new java.awt.Color(255, 255, 255));
        jCheckBoxTaskFinished.setFont(FreeDoitConfig.getFont(0, 11));
        jCheckBoxTaskFinished.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jCheckBoxTaskFinishedActionPerformed(evt);
            }
        });

        jLabelTaskStatus.setFont(FreeDoitConfig.getFont(0, 13));
        jLabelTaskStatus.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
        jLabelTaskStatus.setHorizontalTextPosition(javax.swing.SwingConstants.RIGHT);

        jLabelTaskName.setBackground(new java.awt.Color(255, 255, 255));
        jLabelTaskName.setFont(FreeDoitConfig.getFont(0, 13));

        jLabelDate.setFont(FreeDoitConfig.getFont(0, 13));

        jLabelDateGap.setPreferredSize(new java.awt.Dimension(15, 0));

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(14, 14, 14)
                .addComponent(jCheckBoxTaskFinished)
                .addGap(0, 0, 0)
                .addComponent(jLabelDate)
                .addGap(0, 0, 0)
                .addComponent(jLabelDateGap, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(0, 0, 0)
                .addComponent(jLabelTaskName, javax.swing.GroupLayout.DEFAULT_SIZE, 571, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jLabelTaskStatus, javax.swing.GroupLayout.PREFERRED_SIZE, 66, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(10, 10, 10))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jCheckBoxTaskFinished, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 37, Short.MAX_VALUE)
            .addComponent(jLabelTaskStatus, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
            .addComponent(jLabelTaskName, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
            .addComponent(jLabelDate, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
            .addComponent(jLabelDateGap, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
    }// </editor-fold>//GEN-END:initComponents

    private void jCheckBoxTaskFinishedActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jCheckBoxTaskFinishedActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_jCheckBoxTaskFinishedActionPerformed
                                                
    protected void hideJLabelDateGap() {
        java.awt.Dimension d = jLabelDateGap.getPreferredSize();
        d.width = 0;
        jLabelDateGap.setPreferredSize(d);
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JCheckBox jCheckBoxTaskFinished;
    private javax.swing.JLabel jLabelDate;
    private javax.swing.JLabel jLabelDateGap;
    private javax.swing.JLabel jLabelTaskName;
    private javax.swing.JLabel jLabelTaskStatus;
    // End of variables declaration//GEN-END:variables
}
